#coding=utf-8

import random
import string
import time
import json
import datetime

import decimal
def str_random(len):
    str_list = [random.choice(string.digits + string.ascii_letters) for i in range(len)]
    random_str = ''.join(str_list)
    return random_str
def str_time():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def str_timestamp(isShort=False):
    return str(int_timestamp(isShort))
def int_timestamp(isShort=False):
    if isShort:
        return int(time.time())
    return int(time.time()*1000)
def genName():
    baijiaxing = u'赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫柯房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊于惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭历戎祖武符刘景詹束龙叶幸司韶郜黎蓟溥印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阳郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居衡步都耿满弘匡国文寇广禄阙东欧殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后荆红游竺权逮盍益桓公'
    i = random.randint(0, len(baijiaxing))
    temp = baijiaxing[i:i + 1]
    temp = temp + genChinese(2)
    return temp

def genChinese(num):
    temp = ""
    for n in range(1, num + 1, 1):
        temp = temp + num2Chinese(random.randint(0, 9))
    return temp

def num2Chinese(num):
    temp = ""
    if num == 0:
        temp = "零"
    elif num == 1:
        temp = "一"
    elif num == 2:
        temp = "二"
    elif num == 3:
        temp = "三"
    elif num == 4:
        temp = "四"
    elif num == 5:
        temp = "五"
    elif num == 6:
        temp = "六"
    elif num == 7:
        temp = "七"
    elif num == 8:
        temp = "八"
    elif num == 9:
        temp = "九"
    return temp


# 随机生成手机号码
def genPhone(prefix,index):
    phoneNo=''
    if len(prefix) + len(index) >11:
        phoneNo = prefix[0:11-len(index)]+index
    else:
        phoneNo = prefix.ljust(11-len(index),'0')+index
    return phoneNo

def calcCrfuid(crfuid):
    return str(abs(getHashCode(crfuid))% 10)

def convert_n_bytes(n, b):
    bits = b * 8
    return (n + 2 ** (bits - 1)) % 2 ** bits - 2 ** (bits - 1)

def convert_4_bytes(n):
    return convert_n_bytes(n, 4)

def getHashCode(s):
    h = 0
    n = len(s)
    for i, c in enumerate(s):
        h = h + ord(c) * 31 ** (n - 1 - i)
    return convert_4_bytes(h)
def makeCrfuid(idno):
    pass




class MyCusEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        elif isinstance(obj, datetime.timedelta):
            return (datetime.datetime.min + obj).time().isoformat()
        elif isinstance(obj, decimal.Decimal):
            return str(obj)
        raise json.dumps(obj)
